import config from './config.js';

const errTip = {
  1: '出现未知错误',
  10001: 'appKey错误'
}

class HttpRequest{
  request(params){
    uni.request({
      url: config.baseURL + params.url,
      method: params.method || 'GET',
      header:{
        'content-type': 'application/json',
        appKey: config.appKey
      },
      data: params.data,
      success: res => {
        let code = res.statusCode.toString();
        if(code.startsWith('2')){
          params.success && params.success(res.data);
        } else {
          let errCode = res.data.error_code;
          this._showErrorCode(errCode);
        }
      },
      fail: () => {},
      complete: () => {}
    });
  }
  _showErrorCode(errCode){
    if(!errCode){
      uni.showToast({
        title: errTip[1],
        icon:'warn',
        duration: 1800
      });
    } else {
      uni.showToast({
        title: errTip[errCode],
        icon:'none',
        duration:1800
      })
    }
  }
}

export default HttpRequest;